package org.example.http;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 脚本处理器
 */
public class LoadScriptHandler extends SimpleChannelInboundHandler<FullHttpRequest> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception {
        // TODO 获取数据库类型
        Map<String, List<String>> queryMaps = toQueryParams(request);
        List<String> values = queryMaps.get("dbType");

        String dbType = "";
        if (values == null || values.size() == 0)
            dbType = "mysql";

        dbType = values.get(0);

        String msg = "插入成功";

        switch (dbType) {
            case "mysql":
                DataHandler.mysqlBatch(1, 10, "jizy_flink", Arrays.asList("id", "createtime", "updatetime"));
                break;
            case "mssql":
                DataHandler.sqlserBatch(1, 10);
                break;
            case "oracle":
                DataHandler.oracleBatch(1, 10);
                break;
            case "site_user":
                // TODO site_user
                DataHandler.insertSiteUser(1, 10, "site_user", Arrays.asList("user_name", "user_id", "idcard", "telphone", "xqcode"));
                break;
            case "site_access":
                DataHandler.insertSiteAccess(1, 1000, "site_access", Arrays.asList("datekey", "site_id", "city_code", "user_name", "user_id", "pv"));
                break;
            case "query":
                msg = DataHandler.querySiteAccess() + "";
                break;
            case "query1":
                DataHandler.query1();
                break;
            case "query2":
                DataHandler.query2();
                break;
            case "query3":
                DataHandler.query3();
                break;
            case "query4":
                DataHandler.query4();
                break;
            default:
                DataHandler.mysqlBatch(1, 10, "jizy_flink", Arrays.asList("id", "createtime", "updatetime"));
                break;
        }

        // TODO 输出到服务端信息
        writeMsg(ctx, msg);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();

        // TODO 报错之后，连接关闭
        ctx.close();
    }

    /**
     * @return
     */
    public Map<String, List<String>> toQueryParams(FullHttpRequest request) {
        QueryStringDecoder quertString = new QueryStringDecoder(request.uri());
        return quertString.parameters();
    }

    /**
     * 输出信息到客户端
     *
     * @param message
     */
    public void writeMsg(ChannelHandlerContext ctx, String message) {
        FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);

        byte[] contents = message.getBytes(CharsetUtil.UTF_8);
        response.headers().add("Content-Type", "application/json;charset=utf-8");
        response.headers().add("Content-Length", contents.length);

        response.content().writeBytes(contents);

        ctx.writeAndFlush(response);
    }
}
